package com.libinyn.pdf.controller;


import com.libinyn.pdf.model.ImsPpo;
import com.libinyn.pdf.model.ImsPpoItems;
import com.libinyn.pdf.utils.PrintPdfUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * pdf文件打印
 *
 */
@Controller
@RequestMapping("/")
public class PrintPdfController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 文件  http://localhost:8080/pdf/printpdf/freemarker
     * @param response
     */
    @RequestMapping(value = "/freemarker")
    public void printPdf(HttpServletResponse response){

        ImsPpo imsPpo = getImsPpo();

        PrintPdfUtils.printPdf(response,"printImsPpo.ftl",imsPpo);

    }

    @RequestMapping(value = "/")
    public String index(Map<String, Object> model){
        ImsPpo imsPpo = getImsPpo();
        model.put("imsPpo",imsPpo);
        return "index";
    }

    /**
     * 获取数据
     * @return
     */
    private ImsPpo getImsPpo(){
        ImsPpo imsPpo = new ImsPpo();
        imsPpo.setId("CG2017112701");
        imsPpo.setBllDate(new Date());
        imsPpo.setBllDueDate(new Date());
        imsPpo.setComName("码农科技");
        imsPpo.setEmpChiName("张三");
        imsPpo.setRemark("采购订单");
        imsPpo.setTtlAmt(234.22);

        List<ImsPpoItems> imsPpoItems = new ArrayList<>();
        ImsPpoItems item = new ImsPpoItems();
        item.setId(12);
        item.setProductId("23423");
        item.setItmName("王老吉");
        item.setItmQty(23);
        item.setUnitGroupName("件");
        item.setUnitPrice(3454);
        item.setBllId("CG2017112701");

        ImsPpoItems item2 = new ImsPpoItems();
        item2.setId(33);
        item2.setProductId("2377423");
        item2.setItmName("加多宝");
        item2.setItmQty(54);
        item2.setUnitGroupName("件");
        item2.setUnitPrice(23);
        item2.setBllId("CG2017112701");

        imsPpoItems.add(item);
        imsPpoItems.add(item2);
        imsPpo.setImsPpoItems(imsPpoItems);

        return imsPpo;
    }

}
